package com.dlc.felear.xiaoensale.dao;

import android.content.Context;
import android.util.Log;

import com.dlc.felear.xiaoensale.entity.FlavourSpeed;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FlavourSpeedDao {
    private static final String TAG="FlavourSpeedDao";
    private Dao<FlavourSpeed, Integer> PipeInfoDao;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     *
     * @param context
     */
    public FlavourSpeedDao(Context context) {
        try {
            PipeInfoDao = DetailDataOpenHelper.getInstance(context).getDao(FlavourSpeed.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一条记录
     *
     * @param PipeInfo
     */
    public int update(FlavourSpeed PipeInfo) {
        int temp = -1;
        try {
            Log.e(TAG, "update name=" + JsonHelper.toJson(PipeInfo));
            temp = PipeInfoDao.update(PipeInfo);
        } catch (SQLException e) {
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
        }
        return temp;
    }

    public int add(FlavourSpeed PipeInfo) {
        int temp = -1;
        try {
            Log.e(TAG, "add name=" + JsonHelper.toJson(PipeInfo));
            temp=PipeInfoDao.create(PipeInfo);
        } catch (SQLException e) {
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
        }
        return temp;
    }

    /**
     * 添加多个记录
     *
     * @param list
     */
    public void addAll(List<FlavourSpeed> list) {
        try {
            Log.e(TAG,"生成记录："+JsonHelper.toJson(list));
            for (FlavourSpeed FlavourSpeed : list) {
                if (queryForId(FlavourSpeed.getId()) == null) {
                    add(FlavourSpeed);
                }else {
                    Log.e(TAG,"记录已经存在:"+JsonHelper.toJson(FlavourSpeed));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG,e.getMessage());
        }
    }


    /**
     * 删除一条记录
     *
     * @param PipeInfo
     */
    public void delete(FlavourSpeed PipeInfo) {
        try {
            PipeInfoDao.delete(PipeInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 查询一条记录
     *
     * @param id
     * @return
     */
    public FlavourSpeed queryForId(String id) {
        FlavourSpeed PipeInfo = null;
        try {
            Log.e(TAG,"FlavourSpeed ID:"+id);
            Log.e(TAG,"FlavourSpeeds:"+JsonHelper.toJson(PipeInfoDao.queryForAll()));
            List<FlavourSpeed> tempList = PipeInfoDao.queryBuilder().where().eq("id", id).query();
            if(tempList!=null&&!tempList.isEmpty()){
                PipeInfo=tempList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfo;
    }

    public FlavourSpeed queryForName(String name) {
        FlavourSpeed PipeInfo = null;
        try {
            List<FlavourSpeed> tempList = PipeInfoDao.queryBuilder().where().eq("name", name).query();
            if(tempList!=null&&!tempList.isEmpty()){
                PipeInfo=tempList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfo;
    }
    public  List<FlavourSpeed> queryForType(int id) {
        List<FlavourSpeed> tempList = null;
        try {
           tempList = PipeInfoDao.queryBuilder().where().eq("type", id).query();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tempList;
    }


    /**
     * 查询所有记录
     *
     * @return
     */
    public List<FlavourSpeed> queryForAll() {
        List<FlavourSpeed> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }


}
